package com.wuhan.retrosnaker.controller;

import com.wuhan.retrosnaker.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author wuhan
 * @date 2019/11/18 16:29
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/userLogin")
    @ResponseBody
    public int userLogin(@RequestBody Map<String,String> userInfo, HttpServletRequest request){
        List<Map<String, String>> maps = userService.loginValid(userInfo);
        if (maps.size()>0){

            request.getSession().setAttribute("username",userInfo.get("username"));
            return 1;
        } else {
            return 0;
        }

    }
    @RequestMapping("/userLogout")
    public void Logout(HttpServletRequest request, HttpServletResponse response){
        request.getSession().setAttribute("username",null);
        try {
            response.sendRedirect("/");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @PostMapping("/submitScore")
    @ResponseBody
    public boolean submitScore(@RequestBody Map<String,String> submitInfo){
        int created = userService.isCreated(submitInfo);
        if (created>0){
            return userService.submitScore(submitInfo);
        }else {
            return userService.insertScore(submitInfo);
        }
    }
    @PostMapping("/userSignUp")
    @ResponseBody
    public boolean userSignUp(@RequestBody  Map<String,String> submitInfo){

        return  userService.signUp(submitInfo);
    }

    @GetMapping("/getRankList")
    @ResponseBody
    public  List<Map<String, String>>getRankList(){

        return userService.getRankList();
    }

}
